package com.mycompany.firstmavenproject;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestPU");
        EntityManager em = emf.createEntityManager();
//        EntityManager em2 = emf.createEntityManager();
//        EntityManager em3 = emf.createEntityManager();
        
        Person father = new Person();
        
        father.setFirstName("Paul");
        father.setLastName("Smith");
        father.setGender(Gender.Male);
        father.setAddress("Botanicka 68a, Brno");
        
        em.getTransaction().begin();
        
        em.persist(father);
        
        em.getTransaction().commit();

        dump(em);
        
        Person p = new Person();
        
        p.setFirstName("Paul");
        p.setLastName("Smith Jr.");
        p.setGender(Gender.Male);
        p.setAddress("Botanicka 68a, Brno");
        p.setFather(father);
        
        em.getTransaction().begin();
        
        em.persist(p);
        
        em.getTransaction().commit();
        
        dump(em);
        
        em.close();
//        
//        Person p2 = em2.find(Person.class, p.getId());
//        em2.getTransaction().begin();
//        p2.setAddress("Chvalovice 12");
//        em2.getTransaction().commit();
//        
//        Person p3 = em3.find(Person.class, p2.getId());
//        em3.getTransaction().begin();
//        p3.setLastName("Brown");
//        em3.getTransaction().commit();
//        
//        dump(em);
        
    }
    
    private static void dump(EntityManager manager) {
        String s = manager.createQuery("SELECT p FROM Person p").getResultList().toString();
        System.out.println(s);
        
    }
}
